let hostname = window.location.hostname;
console.log(hostname);
if (hostname === '127.0.0.1') {
  axios.defaults.baseURL = "http://localhost:8000";
}

/**
 * axios拦截器
 */
axios.interceptors.request.use(function (request) {
  console.log('请求参数：', request);
  const user = store.state.user;
  if (Tool.isEmpty(user)) {
    return request
  }
  const token = store.state.user.token;
  if (Tool.isNotEmpty(token)) {
    request.headers.token = token;
    console.log("请求headers增加token:", token);
  }
  return request;
}, error => {
  return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
  console.log('返回结果：', response);
  return response;
}, error => {
  console.log("返回拦截：", error);
  const response = error.response;
  const status = response.status;
  if (status === 401) {
    // 判断状态码是401 跳转到登录
    console.log("未登录，跳到登录页面");
    store.setMember({});
    window.location.href="/admin/login";
  }
  return {
    data: {
      success: false,
      message: "服务器出现异常"
    }
  };
});
